package com.ruoyi.quartz.service.impl;

import com.ruoyi.quartz.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;


@Component
public class MailServiceImpl implements MailService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private JavaMailSender mailSender;

    @Override
    public void sendSimpleMail(String to, String subject, String content) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            // 使用 MimeMessageHelper 来构造HTML邮件
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("1598289826@qq.com");
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true); // 第二个参数设置为 true 表示发送 HTML 格式的内容

            mailSender.send(message);
            logger.info("HTML格式邮件已经发送。");
        } catch (MessagingException e) {
            logger.error("发送HTML邮件时发生异常！", e);
        }

    }
}
